import SimpleOpenNI.*;
SimpleOpenNI kinect;

ArrayList<PVector> handPositions;

PVector currentHand;
PVector previousHand;

int x;
int y;
int posx = 460;
int posy = 160;
int delta = 20;
int step = 1;
int previousStep = -2;
int nstep = 2;
int posExitX = 70;
int posExitY = 380;
int posBackX = 550;
int posBackY = 390;
PImage small, large, back, myexit;
int count, countBack, countExit;
int xp;
int yp;
boolean changeBG = true;
boolean changeExit = true;
boolean changeBack = true;
boolean stepChanged = true;

void setup(){
  small = loadImage("map/map.jpg");
  large = loadImage("map/map star l.jpg");
  back = loadImage("map/map back l.jpg");
  myexit = loadImage("map/map exit l.jpg");
  
  size(960,700,P3D);
  
  kinect = new SimpleOpenNI(this);
  kinect.setMirror(true);
  
  kinect.enableDepth();
  kinect.enableGesture();
  kinect.enableHands();
  
  kinect.addGesture("RaiseHand");

  handPositions = new ArrayList();
  
  stroke(0,0,0);
  strokeWeight(20);
}

void draw(){
  kinect.update();
  scale(1.5f);
  background(small);
  //image(kinect.depthImage(),0,0);
  
  count = 0;
  countBack = 0;
  countExit = 0;
  for(int i=1;i< handPositions.size(); i++) 
  {
    currentHand = handPositions.get(i);
    previousHand = handPositions.get(i-1);
    //fill(255,0,0);
    //ellipse(previousHand.x,previousHand.y,15,15);
    x = int(currentHand.x);
    y = int(currentHand.y);
    xp = int(previousHand.x);
    yp = int(previousHand.y);
    
    if (stepChanged)
    {
      if (step == -2) //exit
      {
        exit();        
      }
      if (step == -1) //back
      {
        step = previousStep;
        stepChanged = true;
      }
      if (step == 1)
      {
        posx = 460;
        posy = 160;
        small = loadImage("map/map.jpg");
        large = loadImage("map/map star l.jpg");
        back = loadImage("map/map back l.jpg");
        myexit = loadImage("map/map exit l.jpg");
        delta = 20;
        previousStep = -2;
      }
      if (step == 2)
      {
        posx = 470;
        posy = 390;
        large = loadImage("qa1 large.jpg");
        small = loadImage("qa1 small.jpg");
        delta = 50;
        nstep = 3;
        previousStep = 1;
      }
      
      if (step == 3)
      {
        posx = 100;
        posy = 230;
        large = loadImage("qa1a large.jpg");
        small = loadImage("qa1a small.jpg");
        delta = 50;
        nstep = 4;
        previousStep = 2;
      }
      
      if (step == 4)
      {
        posx = 0;
        posy = 0;
        large = loadImage("qa2 large.jpg");
        small = loadImage("qa2 small.jpg");
        delta = 50;
        nstep = 5;
        previousStep = 3;
      }
   
      if (step == 5)
      {
        posx = 100;
        posy = 230;
        large = loadImage("qa2a large.jpg");
        small = loadImage("qa2a small.jpg");
        delta = 50;
        nstep = 6;
        previousStep = 4;
      }
      if (step == 6)
      {
        posx = 470;
        posy = 460;
        large = loadImage("sinai large.jpg");
        small = loadImage("sinai small.jpg");
        delta = 50;
        nstep = 7;
        previousStep = 5;
      }

      count = 0;
      xp = -1;
      yp = -1;
      stepChanged = false;
    }
    
    
    // this checks that the hand position is between the values (318,373) and (338,393), you should change these values according to the position where the clicking occurs
    if (x > posBackX-delta && x < posBackX+delta && y > posBackY-delta && y < posBackY+delta && xp > posBackX-delta && xp < posBackX+delta && yp > posBackY-delta && yp < posBackY+delta)
    {
     countBack++;
     // this changes the background to black (represented by the value zero), you can replace this with an image let's say the image is in a variable called newIMG, then you would write background(newIMG)
     if (changeBG) background(back);
     else background(small);
     changeBG = !changeBG; 
    }
//    else background(small);

    if (countBack >= 10) 
    {
      step = -1;
      stepChanged = true;
    }

    
    if (x > posExitX-delta && x < posExitX+delta && y > posExitY-delta && y < posExitY+delta && xp > posExitX-delta && xp < posExitX+delta && yp > posExitY-delta && yp < posExitY+delta)
    {
     countExit++;
     // this changes the background to black (represented by the value zero), you can replace this with an image let's say the image is in a variable called newIMG, then you would write background(newIMG)
     if (changeBG) background(myexit);
     else background(small);
     changeBG = !changeBG;
    }
//    else background(small);

    if (countExit >= 10) 
    {
      step = -2;
      stepChanged = true;
    }

    
    if (x > posx-delta && x < posx+delta && y > posy-delta && y < posy+delta && xp > posx-delta && xp < posx+delta && yp > posy-delta && yp < posy+delta)
    {
     count++;
     // this changes the background to black (represented by the value zero), you can replace this with an image let's say the image is in a variable called newIMG, then you would write background(newIMG)
     if (changeBG) background(large);
     else background(small);
     changeBG = !changeBG; 
    }
//    else background(small);
    
    if (count >= 10) 
    {
      step = nstep;
      stepChanged = true;
    }

    //ellipse(x,y,15,15);
  }
  println("x="+x);
  println("y="+y);
  println(step);
  fill(0,0,0);
  ellipse(x,y,15,15);

  }

void onCreateHands(int handId, PVector position, float time){
  PVector position2= new PVector(position.y,position.x);
  handPositions.add(position);
  position.x = position.x * 1.2;
  position.y = position.y * 1.2;
  
  kinect.convertRealWorldToProjective(position,position);

}

void onUpdateHands(int handId, PVector position, float time){
  PVector position2= new PVector(position.y,position.x);
  handPositions.add(position);
  position.x = position.x * 1.2;
  position.y = position.y * 1.2;
  
  kinect.convertRealWorldToProjective(position,position);
}

void onDestroyHands(int handId, float time) {
  handPositions.clear();
  kinect.addGesture("RaiseHand");
}

void onRecognizeGesture(String strGesture, PVector idPosition, PVector endPosition) {
  kinect.startTrackingHands(endPosition);
  kinect.removeGesture("RaiseHand");
}

